﻿using System;
using System.Web.UI;
using Library.Logging;
using ITViet.Domains;
using System.Web.UI.WebControls;

namespace ITViet.Web.App_Code
{
  public class AdminBaseControl : UserControl
  {
    #region Properties
    public string SelectedMenuItem
    {
      get { return ((AdminBasePage)Page).SelectedMenuItem; }
      set { ((AdminBasePage)Page).SelectedMenuItem = value; }
    }

    public string SelectedMenuItemTitle
    {
      get { return ((AdminBasePage)Page).SelectedMenuItemTitle; }
      set { ((AdminBasePage)Page).SelectedMenuItemTitle = value; }
    }

    public string SelectedSubMenuItem
    {
      get { return ((AdminBasePage)Page).SelectedSubMenuItem; }
      set { ((AdminBasePage)Page).SelectedSubMenuItem = value; }
    }

    public string SelectedSubMenuItemTitle
    {
      get { return ((AdminBasePage)Page).SelectedSubMenuItemTitle; }
      set { ((AdminBasePage)Page).SelectedSubMenuItemTitle = value; }
    }

    public int SelectedMenuItemIndex
    {
      get { return ((AdminBasePage)Page).SelectedMenuItemIndex; }
      set { ((AdminBasePage)Page).SelectedMenuItemIndex = value; }
    }

    protected string DefaultLoggingCategory
    {
      get { return AdminBasePage.DefaultLoggingCategory; }
    }

    public Language CurrentLanguage
    {
      get { return ((AdminBasePage)Page).CurrentLanguage; }
      set { ((AdminBasePage)Page).CurrentLanguage = value; }
    }

    public User CurrentUser
    {
      get { return ((AdminBasePage)Page).CurrentUser; }
      set { ((AdminBasePage)Page).CurrentUser = value; }
    }

    public bool CanApprove
    {
      get { return ((AdminBasePage)Page).CanApprove; }
      set { ((AdminBasePage)Page).CanApprove = value; }
    }

    public bool CanModify
    {
      get { return ((AdminBasePage)Page).CanModify; }
      set { ((AdminBasePage)Page).CanModify = value; }
    }

    public bool CanDelete
    {
      get { return ((AdminBasePage)Page).CanDelete; }
      set { ((AdminBasePage)Page).CanDelete = value; }
    }

    public bool CanView
    {
      get { return ((AdminBasePage)Page).CanView; }
      set { ((AdminBasePage)Page).CanView = value; }
    }
    #endregion

    #region Override Base
    protected override void OnInit(EventArgs e)
    {

    }

    protected override void OnLoad(EventArgs e)
    {
      // Intentionally pauses server-side processing, to demonstrate the Loading Panel functionality.
      if (Page.IsCallback)
        System.Threading.Thread.Sleep(100);

      base.OnLoad(e);
      AdminBasePage basePage = Page as AdminBasePage;
      string curUrl = Request.Url.AbsoluteUri.ToLower();
      if (basePage == null) return;
      if (basePage.CurrentUserPermission == null || curUrl.Contains("errorpage.aspx")) return;
      if (basePage.RequiredPermission >= 0 && !basePage.CanView)
      {
        basePage.ErrorMessage = Resources.Messages.ERR0027;
        Response.Redirect("~/Admin/ErrorPage.aspx");
      }
      else
      {
        ((AdminBasePage)Page).SetPermission();
        if (Page.Master != null)
        {
          Literal ltrMainTitle = Page.Master.FindControl("lrPageTitle") as Literal;
          if (ltrMainTitle != null)
          {
            ltrMainTitle.Text = !string.IsNullOrEmpty(SelectedSubMenuItemTitle) ? SelectedSubMenuItemTitle : SelectedMenuItemTitle;
          }
        }
      }
    }
    #endregion

    #region Methods
    public void ClearSession(string[] ssName)
    {
      ((AdminBasePage)Page).ClearSession(ssName);
    }

    public void WriteLog(string description, Exception ex)
    {
      Logger.LogError(description, ex, DefaultLoggingCategory, null);
    }

    protected string FormatDate(object date)
    {
      DateTime value = DateTime.MinValue;
      DateTime.TryParse(date.ToString(), out value);
      if (value > DateTime.MinValue)
        return value.ToString();
      return "";
    }

    public string GetItemDescription(object status, string path)
    {
      return ((Page) as AdminBasePage).GetItemDescription(status, path);
    }
    #endregion
  }
}
